import {
  _path,
  Write_player,
  existplayer,
  _item,
  bound,
  Write_iteam,
  _yaml
} from '../../mode/allapi.js'
import { plugin, combat, AMessage } from '../../api/api.js'
import {
  setgenerate,
  addnum,
  addexper,
  zhandou,
  setenmy,
  rivalsettlement,
  settlement,
  levy,
  jian
} from './challengeapi.js'

export class figing extends plugin {
  constructor() {
    super({
      dsc: '攻占',
      rule: [
        {
          reg: /^(#|\/)?讨伐贼寇*$/,
          fnc: 'cesi'
        },
        {
          reg: /^(#|\/)?攻占.*$/,
          fnc: 'figg'
        }
      ]
    })
  }
  async cesi(e: AMessage) {
    const id = e.user_id
    if ((await existplayer(id)) != true) return false
    const player = await (await _path(id)).player
    const { imgs, enmy } = await setgenerate(player, undefined, 1)
    if (imgs) e.reply(imgs)
    const logs = await jian(player.wujian)
    if (logs.length != 0) {
      return e.reply(logs)
    }
    const win = await combat(enmy, player.wujian)
    if (win.win != 'playerwin' && win.win != 'enmywin')
      return e.reply('战斗错误')
    const img = await zhandou(win)
    if (img) e.reply(img)
    const log = await addnum(player, win)
    await addexper(player, enmy, win.md, win.dihp)
    Write_player(id, player)
    e.reply(log)
    return false
  }

  async figg(e: AMessage) {
    const id = e.user_id
    if ((await existplayer(id)) != true) return false
    const thing_user = e.msg
    const didian = thing_user.replace(/(#|\/)?攻占/, '')
    const place = await _item(id, 'place')
    const player = await (await _path(id)).player
    const action = await bound(didian, place, 'name')
    if (action == undefined) return e.reply(`没有${didian}`)
    if (player.position == 0) {
      player.position = didian
    } else if (player.position != didian) {
      return e.reply(`请主公军队先到达${didian}`)
    }
    let rival: any
    const rivalid = action.belong
    if (rivalid != '0') {
      rival = await (await _path(rivalid)).player
    }
    if (rivalid == player.user_id) return e.reply('此地早已为主公城池')

    if (action.garrison == 1) {
      const garrison = rival.wujian
      e.reply(`此城池有军队驻守`)
      if (
        garrison &&
        garrison[4] != undefined &&
        garrison[4].name != undefined &&
        garrison[4].hp > 0
      ) {
        const { imgs } = await setgenerate(rival, player.wujian, 0)
        if (imgs) e.reply(imgs)
        const logs = await jian(player.wujian)
        if (logs.length != 0) {
          return e.reply(logs)
        }
        const win = await combat(garrison, player.wujian)
        if (win.win != 'playerwin' && win.win != 'enmywin')
          return e.reply('战斗错误')
        await addexper(player, rival.wujian, win.md, win.dihp)
        await addexper(rival, player.wujian, win.dimd, win.hp)
        const img = await zhandou(win)
        if (img) e.reply(img)
        Write_player(id, player)
        Write_player(rivalid, rival)
        if (win.win == 'enmywin') {
          rival.garrison == 0
          e.reply(`已击败对方镇守军`)
        } else {
          return e.reply(`很遗憾，对方驻守军过强，主公军队溃败`)
        }
      } else {
        e.reply('驻军未配置主将，无法进入战斗')
      }
    }
    const enmy1 = await setenmy(action, id)
    const { imgs, enmy } = await setgenerate(player, enmy1, 0)
    if (imgs) e.reply(imgs)
    const logs = await jian(player.wujian)
    if (logs.length != 0) {
      return e.reply(logs)
    }
    const win = await combat(enmy, player.wujian)
    const log = []
    if (win.win != 'playerwin' && win.win != 'enmywin') {
      return e.reply('战斗错误')
    }
    const img = await zhandou(win)
    if (img) e.reply(img)
    await addexper(player, enmy, win.md, win.dihp)
    if (win.win == 'enmywin') {
      if (rivalid != '0') {
        await levy(rival)
        await rivalsettlement(rival, action)
        Write_player(rivalid, rival)
      }
      await levy(player)
      log.push(await settlement(player, action))
    } else {
      log.push(`很遗憾,主公军队溃败`)
    }
    Write_player(id, player)
    Write_iteam(id, 'place', place)
    e.reply(log)
    return false
  }
}
